<?php

class GaAPI_Services_CampaignTarget extends GaAPI_Services_Abstract {

    protected $_serviceName = 'CampaignTargetService';
    protected $_targetLists = array('AdSchedule', 'Demographic', 'Geo', 'Language', 'Network', 'Platform');

    public function  __call($name,  $arguments) {
        if (strpos($name, 'new') !== false && strpos($name, 'Target') != false) {
            $listType = str_replace('new', '', $name);
            $listType = str_replace('TargetList', '', $listType);
            if (in_array($listType, $this->_targetLists)) {
                $class = 'GaAPI_Objects_' . $listType . '_Target_List';
                GaAPI_Autoloader::instance()->load($class);
                $list = new $class();
                return $list;
            }
            $this->throwError('Invalid Target List requested [' . $listType . ']');
        }

        parent::__call($name, $arguments);
    }


    protected function call($name, $params = false) {
        if ($this->getApi()->checkParam('client_email')) {
            return parent::call($name, $params);
        }
        return $this->throwError("AdGroups methods require clients_email to be set");
    }

    /**
     * @return GaAPI_Objects_Campaign_Target_Operation
     */
     public function newOperation() {
        GaAPI_Autoloader::instance()->load('GaAPI_Objects_Campaign_Target_Operation');
        $operation = new GaAPI_Objects_Campaign_Target_Operation();
        return $operation;
    }

    /**
     * @return GaAPI_Objects_Campaign
     */
    public function newGeoTargetList() {
        GaAPI_Autoloader::instance()->load('GaAPI_Objects_Geo_Target_List');
        $list = new GaAPI_Objects_Geo_Target_List();
        $list->setApi($this->getApi());
        return $list;
    }

}